package com.up.upzl.biz.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.up.upzl.biz.domain.Area;
import com.up.upzl.biz.domain.PaiDianRecord;
import com.up.upzl.biz.service.AreaService;
import com.up.upzl.biz.service.PaiDianRecordService;

/**
 * 拍店控制器
 * 
 * @author geeks
 *
 */
@Controller
@RequestMapping("jiangjin")
public class JiangjinRecordController {

	@Autowired
	private PaiDianRecordService paiDianRecordService;

	@Autowired
	private AreaService areaService;

	@Value("${up.site:}")
	private String siteCode;

	@GetMapping("list")
	public String list(Integer area, Integer businessArea, PaiDianRecord.HuanXian huanXian, String phone,
			String wechatId, Date dateBegin, Date dateEnd, PaiDianRecord.JiangjinStatus status, Pageable page,
			Model model) {
		if (status == null) {
			status = PaiDianRecord.JiangjinStatus.待发放;
		}
		Page<PaiDianRecord> list = paiDianRecordService.findByCondition(area, businessArea, huanXian, phone, wechatId,
				dateBegin, dateEnd, status, page);

		if (status.equals(PaiDianRecord.JiangjinStatus.待发放)) {
			Long count = paiDianRecordService.countByJiangjinStatus(PaiDianRecord.JiangjinStatus.待发放);
			BigDecimal basicJJ = null;
			if (count >= 1 && count < 30) {
				basicJJ = new BigDecimal(2);
			} else if (count > 31 && count < 70) {
				basicJJ = new BigDecimal(2.5);
			} else if (count >= 70) {
				basicJJ = new BigDecimal(3);
			}
			for (PaiDianRecord paiDianRecord : list) {
				paiDianRecord.setBasicJJ(basicJJ);
			}
		}

		model.addAttribute("pageList", list);

		Long parentId = null;
		if (siteCode.equals("bj")) {
			parentId = 36L;
		} else if (siteCode.equals("tj")) {
			parentId = 37L;
		} else {
			parentId = 36L;
		}

		List<Area> areas = areaService.findByParentId(parentId);
		model.addAttribute("areas", areas);

		return "jiangjin/list";
	}

	@PostMapping("fafang")
	public @ResponseBody Map<String, String> update(Long[] ids, Model model) {
		List<PaiDianRecord> paiDianRecords = paiDianRecordService.findAll(Arrays.asList(ids));

		Long count = paiDianRecordService.countByJiangjinStatus(PaiDianRecord.JiangjinStatus.待发放);

		BigDecimal basicJJ = null;
		if (count >= 1 && count < 30) {
			basicJJ = new BigDecimal(2);
		} else if (count > 31 && count < 70) {
			basicJJ = new BigDecimal(2.5);
		} else if (count >= 70) {
			basicJJ = new BigDecimal(3);
		}

		for (PaiDianRecord paiDianRecord : paiDianRecords) {
			paiDianRecord.setBasicJiang(basicJJ);
			paiDianRecord.setExtJiang(paiDianRecord.getExtJJ());

			paiDianRecord.setJiangjinStatus(PaiDianRecord.JiangjinStatus.已发放);

			paiDianRecordService.save(paiDianRecord);
		}

		return ImmutableMap.of("status", "ok");
	}

	@GetMapping("compute")
	public @ResponseBody Map<String, String> compute(Long[] ids, Model model) {
		Long count = paiDianRecordService.countByJiangjinStatus(PaiDianRecord.JiangjinStatus.待发放);
		List<PaiDianRecord> paiDianRecords = paiDianRecordService.findAll(Arrays.asList(ids));

		BigDecimal basicJJ = null;
		if (count >= 1 && count < 30) {
			basicJJ = new BigDecimal(2);
		} else if (count > 31 && count < 70) {
			basicJJ = new BigDecimal(2.5);
		} else if (count >= 70) {
			basicJJ = new BigDecimal(3);
		}

		BigDecimal basic = new BigDecimal(0);
		BigDecimal ext = new BigDecimal(0);
		BigDecimal total = new BigDecimal(0);

		for (PaiDianRecord paiDianRecord : paiDianRecords) {
			basic = basic.add(basicJJ);
			ext = ext.add(paiDianRecord.getExtJJ());
			total = total.add(basicJJ).add(paiDianRecord.getExtJJ());
		}

		return ImmutableMap.of("basic", basic.toString(), "ext", ext.toString(), "total", total.toString());
	}
}
